<?php
namespace App\Http\Controllers\ClubLeader;

use App\Club;
use App\ClubLessons;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ClubLessonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $club_id = $request->session()->get('club_id');
        $clessons = ClubLessons::where('club_id',$club_id)->Paginate(8);
        return view('ClubLeader.clubLesson', compact('clessons'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('ClubLeader.createClubLesson');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $club_id = Club::latest('created_at')->first()->value('id');

        $rules = [
            'lesson_name' => 'required',
            'lesson_intro' => 'required|min:6',
            'lesson_price' => 'required',
            'lesson_star' => 'required'
        ];

        $this -> validate($request, $rules);
        $clubLesson = new ClubLessons;
        $clubLesson -> lesson_name = $request -> get('lesson_name');
        $clubLesson -> lesson_intro = $request -> get('lesson_intro');
        $clubLesson -> lesson_price = $request -> get('lesson_price');
        $clubLesson -> lesson_star = $request -> get('lesson_star');
        $clubLesson -> club_id = $club_id;

        $clubLesson -> save();

        session() -> flash('success', '添加俱乐部课程信息成功');

        return redirect() -> route('clubLesson.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $clubLesson = ClubLessons::findOrFail($id);
        return view('ClubLeader.editClubLesson', compact('clubLesson'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $clubLesson = ClubLessons::findOrFail($id);
        $clubLesson -> update([
            'lesson_name' => $request -> get('lesson_name'),
            'lesson_intro' => $request -> get('lesson_intro'),
            'lesson_price' => $request -> get('lesson_price'),
            'lesson_star' => $request -> get('lesson_star')
        ]);
        $clubLesson -> save();

        session() -> flash('success', '更新成功');

        return redirect() -> route('clubLesson.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $clesson = ClubLessons::findOrFail($id);

        $clesson -> delete();
    }
}
